﻿namespace SLClient.CustomerControls
{
    using SLClient.Infrastructure;
    using System;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Windows;
    using System.Windows.Threading;

    public class BonusControlViewModel : ViewModelBase
    {
        private BonusControl _bonusControl;
        private string _bonuseNumber;
        private Visibility _contentVisibility = Visibility.Collapsed;
        private string _currentInfo;
        private PlayData _currentPlayData;
        private PlayDataInfo _currentPlayDataInfo;
        private DispatcherTimer _dispatcherTimer;
        private string _lastInfo;
        private PlayData _lastPlayData;
        private Visibility _loadingVisibility = Visibility.Collapsed;

        public BonusControlViewModel(BonusControl bonusControl, PlayDataInfo currentPlayData)
        {
            this._currentPlayDataInfo = currentPlayData;
            this._bonusControl = bonusControl;
            this._currentPlayData = currentPlayData.CurrentPlayData;
            this._lastPlayData = currentPlayData.LastPlayData;
            this._dispatcherTimer = new DispatcherTimer();
            this._dispatcherTimer.Interval=new TimeSpan(0x3e8);
            this._dispatcherTimer.Tick+=new EventHandler(this.DispatcherTimerTick);
            this._dispatcherTimer.Start();
        }

        private void DispatcherTimerTick(object sender, EventArgs e)
        {
            if ((this.CurrentPlayData != null) && (this.LastPlayData != null))
            {
                if (ServerDateTime.Now < this.CurrentPlayData.StartTime)
                {
                    this._bonusControl.CurrentBonusStatus = BonusStatus.NotStart;
                    this.CurrentInfo = string.Format("{0}期 距离开始时间还有{1}", this.CurrentPlayData.Qh, this.GetTimeSpanString((TimeSpan) (this.CurrentPlayData.StartTime - ServerDateTime.Now)));
                    this.LastInfo = string.Format("{0}期 开奖结果", this.LastPlayData.Qh);
                    this.LoadingVisibility = Visibility.Collapsed;
                    this.ContentVisibility = Visibility.Visible;
                }
                else if (ServerDateTime.Now < this.CurrentPlayData.EndTime)
                {
                    this._bonusControl.CurrentBonusStatus = BonusStatus.StartBet;
                    this.CurrentInfo = string.Format("{0}期 距离封单时间还有{1}", this.CurrentPlayData.Qh, this.GetTimeSpanString((TimeSpan) (this.CurrentPlayData.EndTime - ServerDateTime.Now)));
                    this.LastInfo = string.Format("{0}期 开奖结果", this.LastPlayData.Qh);
                    this.LoadingVisibility = Visibility.Collapsed;
                    this.ContentVisibility = Visibility.Visible;
                }
                else if (ServerDateTime.Now < this.CurrentPlayData.OpenTime)
                {
                    this._bonusControl.CurrentBonusStatus = BonusStatus.EndBet;
                    this.CurrentInfo = string.Format("{0}期 距离开奖时间还有{1}", this.CurrentPlayData.Qh, this.GetTimeSpanString((TimeSpan) (this.CurrentPlayData.OpenTime - ServerDateTime.Now)));
                    this.LastInfo = string.Format("{0}期 开奖结果", this.LastPlayData.Qh);
                    this.LoadingVisibility =Visibility.Collapsed;
                    this.ContentVisibility = Visibility.Visible;
                }
                else if (ServerDateTime.Now > this.CurrentPlayData.OpenTime)
                {
                    if (!(this.CurrentPlayDataInfo.IsLoading || (this._bonusControl.CurrentBonusStatus != BonusStatus.StartOpen)))
                    {
                        this.CurrentPlayDataInfo.IsLoading = true;
                        this._bonusControl.CurrentBonusStatus = BonusStatus.StartOpen;
                    }
                    else if (this._bonusControl.CurrentBonusStatus != BonusStatus.StartOpen)
                    {
                        this.CurrentInfo = "";
                        this._bonusControl.CurrentBonusStatus = BonusStatus.StartOpen;
                        this.ContentVisibility = Visibility.Collapsed;
                        this.LoadingVisibility = Visibility.Visible;
                        if (this.CurrentPlayData.Qh == "")
                        {
                            this.LastInfo = "正在开奖";
                        }
                        else
                        {
                            this.LastInfo = string.Format("{0}期 正在开奖", this.CurrentPlayData.Qh);
                        }
                        this.CurrentPlayDataInfo.IsLoading = true;
                    }
                }
            }
        }

        private string GetTimeSpanString(TimeSpan span)
        {
            if (span.TotalHours > 1.0)
            {
                return string.Format("{0:hh}:{0:mm}:{0:ss}", span);
            }
            if (span.TotalMinutes > 1.0)
            {
                return string.Format("{0:mm}:{0:ss}", span);
            }
            return string.Format("{0:ss}", span);
        }

        public Visibility ContentVisibility
        {
            get
            {
                return this._contentVisibility;
            }
            set
            {
                this._contentVisibility = value;
                base.Notify<Visibility>(() => ContentVisibility);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusControlViewModel)), (MethodInfo)methodof(BonusControlViewModel.get_ContentVisibility)), new ParameterExpression[0]));
            }
        }

        public string CurrentInfo
        {
            get
            {
                return this._currentInfo;
            }
            set
            {
                this._currentInfo = value;
                base.Notify<string>(() => CurrentInfo);
                //base.Notify<string>(System.Linq.Expressions.Expression.Lambda<Func<string>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusControlViewModel)), (MethodInfo)methodof(BonusControlViewModel.get_CurrentInfo)), new ParameterExpression[0]));
            }
        }

        public PlayData CurrentPlayData
        {
            get
            {
                return this.CurrentPlayDataInfo.CurrentPlayData;
            }
            set
            {
                this._currentPlayData = value;
                base.Notify<PlayData>(() => CurrentPlayData);
                //base.Notify<PlayData>(System.Linq.Expressions.Expression.Lambda<Func<PlayData>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusControlViewModel)), (MethodInfo)methodof(BonusControlViewModel.get_CurrentPlayData)), new ParameterExpression[0]));
            }
        }

        public PlayDataInfo CurrentPlayDataInfo
        {
            get
            {
                return this._currentPlayDataInfo;
            }
            set
            {
                this._currentPlayDataInfo = value;
                base.Notify<PlayDataInfo>(() => CurrentPlayDataInfo);
                //base.Notify<PlayDataInfo>(System.Linq.Expressions.Expression.Lambda<Func<PlayDataInfo>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusControlViewModel)), (MethodInfo)methodof(BonusControlViewModel.get_CurrentPlayDataInfo)), new ParameterExpression[0]));
            }
        }

        public string LastInfo
        {
            get
            {
                return this._lastInfo;
            }
            set
            {
                this._lastInfo = value;
                base.Notify<string>(() => LastInfo);
                //base.Notify<string>(System.Linq.Expressions.Expression.Lambda<Func<string>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusControlViewModel)), (MethodInfo)methodof(BonusControlViewModel.get_LastInfo)), new ParameterExpression[0]));
            }
        }

        public PlayData LastPlayData
        {
            get
            {
                return this.CurrentPlayDataInfo.LastPlayData;
            }
            set
            {
                this._lastPlayData = value;
                base.Notify<PlayData>(() => LastPlayData);
                //base.Notify<PlayData>(System.Linq.Expressions.Expression.Lambda<Func<PlayData>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusControlViewModel)), (MethodInfo)methodof(BonusControlViewModel.get_LastPlayData)), new ParameterExpression[0]));
            }
        }

        public Visibility LoadingVisibility
        {
            get
            {
                return this._loadingVisibility;
            }
            set
            {
                this._loadingVisibility = value;
                base.Notify<Visibility>(() => LoadingVisibility);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusControlViewModel)), (MethodInfo)methodof(BonusControlViewModel.get_LoadingVisibility)), new ParameterExpression[0]));
            }
        }
    }
}

